package com.uestc.admin;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.uestc.hbase.MTable;
import com.uestc.util.JsonUtil;

public class UserLogAdmin implements AdminBase {
	private String columnName = "UserLogColumns";
	private String tableName = "userlog";

	@Override
	public String getColumnName() {
		return columnName;
	}

	@Override
	public String getTableName() {
		return tableName;
	}

	@Override
	public String getInfo(int page, int limit, HttpServletRequest request) throws Throwable {
		return this.getDetailInfo(tableName, "UserLogColumns", Arrays.asList(new String[] { "info" }), page, limit,
				request);
	}

	@Override
	public String getInfoWithCondition(int page, int limit, HashMap<String, String> conditions,
			HttpServletRequest request) throws Throwable {
		return this.getDetailInfo("userlog", "UserLogColumns", Arrays.asList(new String[] { "info" }), page, limit,
				request, "info", conditions, MTable.FILTER_TYPE.PREFIX_FILTER);
	}

	/**
	 * 用户日志中会使用到的信息
	 * 
	 * @author justyoung
	 *
	 */
	public static class UserLogInfo {
		public String uploadCounts;
		public String downloadCount;
		public String loginCounts;
		public String uploadMetaDataCounts;
		public String downloadMetaDataCounts;
		public String deleteCounts;

		public UserLogInfo() {
			uploadCounts = "upload files";
			downloadCount = "download files";
			deleteCounts = "delete files";
			loginCounts = "user login";
			uploadMetaDataCounts = "upload metadata";
			downloadMetaDataCounts = "download metadata";
		}

		public String getJsonString() throws IllegalArgumentException, IllegalAccessException {
			HashMap<String, Object> map = new HashMap<>();
			for (Field field : this.getClass().getFields()) {
				map.put(field.getName(), field.get(this));
			}
			return JsonUtil.createJsonString(map);
		}

		public Map<String, String> getMap() throws IllegalArgumentException, IllegalAccessException {
			HashMap<String, String> map = new HashMap<>();
			for (Field field : this.getClass().getFields()) {
				map.put(field.getName(), (String) field.get(this));
			}
			return map;
		}
	}

	/**
	 * 获得用户的行为信息
	 * 
	 * @param t
	 *            查询过去t天的用户行为记录
	 * @return
	 * @throws Throwable
	 */
	public String getUserLog(int t) throws Throwable {
		LocalDate date = LocalDate.now();
		LinkedHashMap<String, Object> map = new LinkedHashMap<>();
		MTable hisTable = new MTable("historylog");
		MTable logTable = new MTable("userlog");
		Field fields[] = UserLogInfo.class.getFields();
		try {
			for (int i = t - 1; i >= 0; --i) {
				LocalDate minusDate = date.minusDays(i);
				UserLogInfo userLogInfo = new UserLogInfo();
				String rowKey = minusDate.toString();
				if (i != 0 && hisTable.isRowExists(rowKey)) { // 如果HistoryLog中存在这些信息，就不要去userlog表里统计了，否则，需要去表里统计
					hisTable.getRowInfo(rowKey);
					for (Field field : fields) {
						String r = hisTable.getValue("info", field.getName());
						field.set(userLogInfo, r);
					}
					map.put(rowKey, userLogInfo.getJsonString());
				} else {
					for (Field field : fields) { // 在userlog表中统计信息
						HashMap<String, String> conditions = new HashMap<>();
						conditions.put("action", (String) field.get(userLogInfo));
						conditions.put(MTable.FILTER_TYPE.ROW_FILTER.getFilterName(), minusDate.toString());
						long r = logTable.getRowCounterWithConditions("userlog", "info", conditions);
						field.set(userLogInfo, String.valueOf(r));
						map.put(rowKey, userLogInfo.getJsonString()); // 把统计的信息写进historylog表，下次直接读即可，注意，当日数据不能存储，因为当日数据还在变化
					}
					if (i != 0)
						hisTable.setData(rowKey, "info", userLogInfo.getMap());
				}
			}
		} finally {
			hisTable.close();
			logTable.close();
		}

		String result = JsonUtil.createJsonString(map);
		return result;
	}

	@Override
	public String getDateColumnName() {
		return "action_time";
	}
}
